package com.team.htcontroller;

import com.github.pagehelper.PageInfo;
import com.team.entity.AppInfo;
import com.team.entity.AppVersion;
import com.team.service.AppInfoService;
import com.team.service.AppVersionService;
import com.team.utils.AppInfoCondition;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;


@Controller(value = "htAppInfoController")
public class AppInfoController {

    @Autowired
    private AppInfoService appInfoService;
    @Autowired
    private AppVersionService appVersionService;

    @RequestMapping("/bkAppInfo")
    public String bkAppInfo(AppInfoCondition appInfoCondition, Model model){
        PageInfo<AppInfo> list = appInfoService.getAppInfoByStatus(appInfoCondition);
        model.addAttribute("appInfoList",list);
        model.addAttribute("condition",appInfoCondition);

        return "jsp/backend/applist";
    }
    /*
    * 点击审核去查询
    * */
    @RequestMapping("/AppInfoChecked")
    public String AppInfoChecked(Long id,Model model){
        AppInfo appInfoForChecked = appInfoService.getAppInfoForChecked(id);
        AppVersion newVersion = appVersionService.getNewVersion(id);
        model.addAttribute("appInfo",appInfoForChecked);
        model.addAttribute("appVersion",newVersion);
        return "jsp/backend/appcheck";
    }

    @RequestMapping("/CheckedAppInfo")
    public String CheckedAppInfo(Long id,Long status){
        int temp = appInfoService.updateAppStatus(id, status);
        if (temp>0){
            return "redirect:bkAppInfo";
        }else {
            return "redirect:AppInfoChecked?id="+id;
        }
    }

    @RequestMapping("/download")
    public void download(String path, HttpServletResponse response)throws Exception{

        /*//写入服务器中的文件,将文件转成二进制
        FileInputStream fis = new FileInputStream(path);
        //设置响应下载文件的报头
        String downloadFileName = System.currentTimeMillis() + ".apk";
        //设置抬头，意思是取一个新的文件名，将读入的文件用这个文件名的内置空间接收，相当于取了一个新的文件名
        response.setHeader("Content-Disposition","attachment;fileName="+downloadFileName);
        //设置下载文件的类型
        response.setContentType("application/vnd.android.package-archive");

        byte[] bt = new byte[fis.available()];

        fis.read(bt);
        fis.close();

        //将文件写出到响应流中
        ServletOutputStream sos = response.getOutputStream();
        sos.write(bt);*/

        FileInputStream fis = new FileInputStream("E:\\hahh.txt");
        byte[] bt = new byte[fis.available()];
        fis.read(bt);
        //fis.close();
        ServletOutputStream sos = response.getOutputStream();
        sos.write(bt);
    }

    @RequestMapping("/upDownApp")
    @ResponseBody
    public String upDownApp(Long appid,Long status){
        int temp = appInfoService.updateAppStatus(appid, status);
        return "{\"result\":"+temp+"}";
    }
}
